﻿(function ($) {

    // jQuery slider plugin definition
    $.fn.imageRotator = function (options) {

        var defaults = {
            Effect: 'scrollLeft',
            Speed: '300',
            Timeout: 3000,
            Delay: 2000,
            PauseOnHover: 1,
            Random: 0,
            Fit: 1,
            ShowPrevNextButton: 0,
            PagerType: 'none'
        };

        var settings = buildSettings(options, defaults);

        var functions = {
            buildBubblePagerElement: function (index, domElement) {
                return "<a href='#'>" + (index + 1) + "</a>";
            }
        };

        // iterate each field
        this.each(function () {
            var container = $(this);
            var slider = container.find("div.image-slider");
            if (slider) {
                var paging = setupPaging(slider, settings);
                $(slider).cycle({
                    fx: settings.Effect,
                    speed: settings.Speed,
                    timeout: settings.Timeout,
                    delay: settings.Delay,
                    pause: settings.PauseOnHover,
                    random: settings.Random,
                    fit: settings.Fit,
                    pager: paging.Pager,
                    pagerAnchorBuilder: paging.PagerBuilder,
                    next: paging.NextButton,
                    prev: paging.PrevButton
                });
                adjustRotatorSize(slider, settings, paging);
            }
        });

        // helper methods
        function setupPaging(slider, sourceOptions) {
            var result = {
                Pager: null,
                PagerBuilder: null,
                PrevNextButtonContainer: null,
                PrevButton: null,
                NextButton: null
            };

            if (sourceOptions.PagerType && sourceOptions.PagerType != 'None') {
                if (sourceOptions.PagerType === 'Bubbles') {
                    var navigation = slider.next(".image-slider-nav");
                    if (navigation.length == 0) {
                        navigation = $("<div class='image-slider-nav bubbles' />");
                        navigation.insertAfter(slider);
                        result.Pager = navigation;
                        result.PagerBuilder = functions.buildBubblePagerElement;
                    }
                }
            }
            if (sourceOptions.ShowPrevNextButton) {
                var prevnextNav = slider.next(".image-slider-prevnext-nav");
                if (prevnextNav.length == 0) {
                    var navigationTemplate =
                    "<div class='image-slider-prevnext-nav'>"
                        + "<div class='prev-btn'><span class='btn-inner'></span></div>"
                        + "<div class='next-btn'><span class='btn-inner'></span></div></div>";
                    prevnextNav = $(navigationTemplate);
                    prevnextNav.insertAfter(slider);
                    result.PrevNextButtonContainer = prevnextNav;
                    result.PrevButton = prevnextNav.find(".prev-btn .btn-inner");
                    result.NextButton = prevnextNav.find(".next-btn .btn-inner");

                    result.PrevButton.hover(
                        function () { $(this).animate({ left: '+=9' }, 100); },
                        function () { $(this).animate({ left: '-=9' }, 100); });
                    result.NextButton.hover(
                        function () { $(this).animate({ right: '+=9' }, 100); },
                        function () { $(this).animate({ right: '-=9' }, 100); });
                }
            }

            return result;
        }

        function buildSettings(sourceOptions, defaultOptions) {
            var result = {};
            if (sourceOptions) {
                if (sourceOptions.Effect)
                    result.Effect = sourceOptions.Effect;
                if (sourceOptions.Timeout)
                    result.Timeout = sourceOptions.Timeout;
                if (sourceOptions.PauseOnHover)
                    result.PauseOnHover = sourceOptions.PauseOnHover ? 1 : 0;
                if (sourceOptions.ShowPrevNextButton)
                    result.ShowPrevNextButton = sourceOptions.ShowPrevNextButton ? 1 : 0;
                if (sourceOptions.PagerType)
                    result.PagerType = sourceOptions.PagerType;

                if (sourceOptions.Speed == "Slow")
                    result.Speed = '150';
                else if (sourceOptions.Speed == "Fast")
                    result.Speed = '450';
                else
                    result.Speed = '300';
            }
            return $.extend({}, defaultOptions, result);
        }

        function adjustRotatorSize(slider, sourceOptions, paging) {
            var firstSlide = slider.children(":first");
            var firstSlideHeight = firstSlide.height();
            var firstSlideWidth = firstSlide.width();

            // fix slider height
            slider.height(firstSlideHeight);

            // fix prev/next navigation width
            if (sourceOptions.ShowPrevNextButton) {
                if (paging.PrevNextButtonContainer) {
                    paging.PrevNextButtonContainer.width(firstSlideWidth);
                }
            }

            if (slider.height() == 0) {
                setTimeout(function () {
                    adjustRotatorSize(slider, sourceOptions, paging);
                }, 200);
            }
        }

        // for jQuery chaining
        return this;
    };
})(jQuery);
